#define _CRT_SECURE_NO_WARNINGS 1
 struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(nullptr) {}
 };
 
class Solution {
public:
    bool hasCycle(ListNode* head)
    {
        if (!head || !head->next) return false;
        ListNode* slow = head, * fast = head->next;
        while (fast && fast->next)
        {
            if (fast == slow) return true;
            fast = fast->next->next;
            slow = slow->next;
        }
        return false;
    }
};